import 'dart:convert';

extension MapEx on Map {
  String? getStr(String key) {
    final v = this[key];
    if (v is String) return v;
    return v.toString();
  }

  int? getInt(String key) => this[key] as int?;
  double? getDouble(String key) {
    final v = this[key];
    if (v is String) {
      return num.tryParse(v)?.toDouble();
    }
    if (v is num) return v.toDouble();
    return null;
  }

  bool? getBool(String key) => this[key] as bool?;
  List<dynamic>? getLst(String key) => this[key] as List<dynamic>?;
  Map<String, dynamic>? getMap(String key) =>
      this[key] as Map<String, dynamic>?;
}

extension JsonMap on Map {
  String get jsonString {
    try {
      return jsonEncode(this);
    } catch (e) {
      return '';
    }
  }
}

///url
extension UrlParm on Map {
  String get queryParms {
    var str = '';
    forEach((key, value) {
      str += '$key=${value.toString()}&';
    });

    return str.substring(0, str.length - 1);
  }
}
